import React, { Suspense } from 'react';
import { HashRouter, Routes, Route, Navigate } from 'react-router-dom';
import { Spin } from 'antd';

// 使用 React.lazy 实现路由懒加载
const LoginPage = React.lazy(() => import('../pages/Login').then(module => ({ default: module.LoginPage })));
const RegisterPage = React.lazy(() => import('../pages/Register').then(module => ({ default: module.RegisterPage })));
const MainApp = React.lazy(() => import('../pages/MainApp').then(module => ({ default: module.MainApp })));
const UpdatePage = React.lazy(() => import('../pages/UpdatePage').then(module => ({ default: module.UpdatePage })));

// 全局加载动画
const FullscreenSpinner: React.FC = () => (
  <div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '100vh' }}>
    <Spin size="large" />
  </div>
);

export const AppRouter: React.FC = () => (
  <HashRouter>
    <Suspense fallback={<FullscreenSpinner />}>
      <Routes>
        {/* 初始加载时，直接显示登录页 */}
        <Route path="/" element={<LoginPage />} />
        <Route path="/login" element={<LoginPage />} />
        <Route path="/register" element={<RegisterPage />} />
        <Route path="/app" element={<MainApp />} />
        <Route path="/update" element={<UpdatePage />} />
        {/* 其他所有未匹配的路径都重定向到登录页 */}
        <Route path="*" element={<Navigate to="/login" replace />} />
      </Routes>
    </Suspense>
  </HashRouter>
); 